package com.meilai.project.controller.web.finance.supplier;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.meilai.project.dto.base.ApiResult;
import com.meilai.project.dto.finance.supplier.SupplierMaterialListDTO;
import com.meilai.project.entity.finance.supplier.SupplierMaterial;
import com.meilai.project.service.finance.supplier.SupplierMaterialService;
import com.meilai.project.vo.finance.supplier.SupplierMaterialWebVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(value = "web端供应物料商控制器", description = "web端供应物料商控制器")
@RestController
@RequestMapping("api/web/supplier_material")
public class WebSupplierMaterialController {

    @Autowired
    private SupplierMaterialService service;

    @GetMapping("list")
    @ApiOperation(value = "列表分页")
    public ApiResult<Page<SupplierMaterialWebVO>> list(@ApiParam("form") SupplierMaterialListDTO req) {
        Page<SupplierMaterialWebVO> voPage = service.selectPageList(req);
        return ApiResult.ok(voPage);
    }

    @PostMapping("edit")
    @ApiOperation("编辑物料")
    public ApiResult<Boolean> edit(@ApiParam("json") @Valid @RequestBody SupplierMaterial req) {
        try {
            if (service.edit(req)) {
                return ApiResult.ok("编辑成功");
            } else {
                return ApiResult.err("编辑失败");
            }
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

    @DeleteMapping("delete")
    @ApiOperation("删除物料")
    public ApiResult<Boolean> delete(@ApiParam("id") @RequestParam(value = "id") Long id) {
        try {
            if (service.delete(id)) {
                return ApiResult.ok("删除成功");
            } else {
                return ApiResult.err("删除失败");
            }
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

}
